package jmu.edu.yjc.service.impl;

import jmu.edu.yjc.Utils.BeanCopyUtil;
import jmu.edu.yjc.Utils.JwtUtil;
import jmu.edu.yjc.Utils.RedisCache;
import jmu.edu.yjc.Utils.SecurityUtil;
import jmu.edu.yjc.constants.SystemConstants;
import jmu.edu.yjc.domain.ResponseResult;
import jmu.edu.yjc.domain.entity.LoginUser;
import jmu.edu.yjc.domain.entity.User;
import jmu.edu.yjc.domain.vo.RoutersVo;
import jmu.edu.yjc.domain.vo.menu.MenuVo;
import jmu.edu.yjc.domain.vo.user.AdminUserInfoVo;
import jmu.edu.yjc.domain.vo.user.UserInfoVo;
import jmu.edu.yjc.domain.vo.user.UserLoginVo;
import jmu.edu.yjc.enums.AppHttpCodeEnum;
import jmu.edu.yjc.exception.SystemException;
import jmu.edu.yjc.service.AdminLoginService;
import jmu.edu.yjc.service.MenuService;
import jmu.edu.yjc.service.RoleService;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class AdminLoginServiceImpl implements AdminLoginService {
    @Resource
    private AuthenticationManager authenticationManager;
    @Resource
    private RedisCache redisCache;
    @Resource
    private MenuService menuService;
    @Resource
    private RoleService roleService;
    @Override
    public ResponseResult userLogin(User user) {
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user.getUserName(),user.getPassword());
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        //判断是否认证通过
        if(Objects.isNull(authenticate)){
            return ResponseResult.errorResult(-1,"用户名或密码错误");
        }
        //获取userid生成token
        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
        if((Integer.parseInt(loginUser.getUser().getType()) <1)){
            return ResponseResult.errorResult(AppHttpCodeEnum.NOT_ADMIN_USER);
        }
        Long id = loginUser.getUser().getId();
        String jwt = JwtUtil.createJWT(id.toString());
        // 把用户信息存入redis
        redisCache.setCacheObject("csamsadminlogin:"+id,loginUser,48, TimeUnit.HOURS);
        UserInfoVo userInfoVo = BeanCopyUtil.copyBeans(loginUser.getUser(), UserInfoVo.class);
        UserLoginVo userLoginVo = new UserLoginVo(jwt,userInfoVo);
        return ResponseResult.okResult(userLoginVo);
    }

    @Override
    public ResponseResult getInfo() {
        //获取当前登录用户id
        Long userId = SecurityUtil.getUserId();
        //根据用户id查登录角色信息
        List<String> perms = menuService.selectPermsByUserId(userId);
        //根据用户id查询角色信息
        List<String> roleKeys = roleService.selectRoleKeyByUserId(userId);
        //根据用户id获取userInfo
        User user = SecurityUtil.getLoginUser().getUser();
        UserInfoVo userInfoVo = BeanCopyUtil.copyBeans(user, UserInfoVo.class);
        //封装数据返回
        AdminUserInfoVo adminUserInfoVo = new AdminUserInfoVo(perms,roleKeys,userInfoVo);
        return ResponseResult.okResult(adminUserInfoVo);
    }

    @Override
    public ResponseResult getRouters() {
        //获取当前登录用户id
        Long userId = SecurityUtil.getUserId();
        //查询menu显示是tree的形式
        List<MenuVo> menuVos = menuService.selectRouterMenuTreeByUserId(userId);
        //封装数据返回
        return ResponseResult.okResult(new RoutersVo(menuVos));
    }

    @Override
    public ResponseResult logout() {
        //获取当前登录的用户id
        Long userId = SecurityUtil.getUserId();
        //删除redis中对应的值
        redisCache.deleteObject("csamsadminlogin:"+userId);
        return ResponseResult.okResult();
    }

}
